<template>

<!---搜索表单-->
    <div class="search-div">
        <el-form label-width="70px" size="small">
            <el-row>
                <el-col :span="12">
                <el-form-item label="运动类别">
                    <el-select
                    class="m-2"
                    placeholder="请选择"
                    size="small"
                    style="width: 100px"
                    v-model="queryDto.sportsId"
                    >
                    <el-option
                        v-for="item in sporList"
                        :key="item.id"
                        :label="item.category"
                        :value="item.id"
                    />
                    </el-select>
                </el-form-item>
                </el-col>
            </el-row>
            <el-row style="display:flex">
                <el-button type="primary" size="small" @click="findMovements">
                    搜索
                </el-button>
                <el-button size="small" @click="resetData">重置</el-button>
            </el-row>
        </el-form>
    </div>

    <!--添加按钮-->
    <div class="tools-div">
        <el-button type="success" size="small" @click="addMovement">添 加</el-button>
    </div>
    <!---数据表格-->
    <el-table :data="list" style="width: 100%">
        <el-table-column type="index" label="序号"/>
        <el-table-column prop="name" label="运动项目" />
        <el-table-column prop="category" label="运动类型" />
        <el-table-column prop="expend" label="单位时间(分)" />
        <el-table-column prop="minutes" label="能量消耗(kcal)" />
        <el-table-column prop="whether" label="是否为常见运动" #default="scope">
            {{ scope.row.whether == 1 ? '是' : '否' }}
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column label="操作" align="center" width="280" #default="scope">
            <el-button type="primary" size="small" @click="editShow(scope.row)">
                编辑
            </el-button>
            <el-button type="danger" size="small" @click="deleteById(scope.row)">
                删除
            </el-button>
        </el-table-column>
    </el-table>

    <!-- 添加/修改 -->
    <el-dialog v-model="dialogVisible" title="添加/修改" width="40%">
        <el-form label-width="120px">
            <el-form-item label="运动类别">
                    <el-select
                    class="m-2"
                    placeholder="请选择"
                    size="small"
                    style="width: 100px"
                    v-model="movement.sportsId"
                    >
                    <el-option
                        v-for="item in sporList"
                        :key="item.id"
                        :label="item.category"
                        :value="item.id"
                    />
                    </el-select>
                </el-form-item>
            <el-form-item label="运动项目">
                <el-input v-model="movement.name"/>
            </el-form-item>
            <el-form-item label="单位时间（分）">
                <el-input v-model="movement.expend"/>
            </el-form-item>
            <el-form-item label="消耗热量（kal）">
                <el-input v-model="movement.minutes"/>
            </el-form-item>

            <el-form-item label="是否为常见运动">
                <el-radio-group v-model="movement.whether">
                    <el-radio :label="1">是</el-radio>
                    <el-radio :label="2">否</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="运动详情">
                <el-input type="textarea" v-model="movement.explain"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submit">提交</el-button>
                <el-button @click="falseAll">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog> 

    <!--分页条-->
    <el-pagination
                    v-model:current-page="pageParams.page"
                    v-model:page-size="pageParams.limit"
                    :page-sizes="[10, 20, 50, 100]"
                    @size-change=""
                    @current-change=""
                    layout="total, sizes, prev, pager, next"
                    :total="total"
        />
</template>

<script setup>
import { ref,onMounted } from 'vue'; 
import {findMovement,DeleteMovement,findSports,addmovement,findMovementById,updateMovement} from '@/api/movement';
import { ElMessage,ElMessageBox } from 'element-plus'

//数据模板
let list = ref([])
//分页总条数
let total = ref(0)

const pageParamsForm = {
    page : 1,
    limit: 3
}

const movement = ref({
    id:'',
    sportsId:'',
    name:'',
    explain:'',
    expend:'',
    minutes:'',
    whether:''
})

const sporList = ref([])

const dialogVisible = ref(false)


const pageParams = ref(pageParamsForm)
// 定义搜索表单数据模型
const queryDto = ref({
    sportsId: "" 
})
onMounted(()=>{
    //查询所有数据
    findMovements()
    //查询所有运动类型
    findSportsAll()
})
const findMovements = async()=>{
    const {data} = await findMovement(queryDto.value)
    list.value = data
}
//重置
const resetData = () => {
  queryDto.value = { sportsId:'' }
  findMovements()
}

const findSportsAll = async()=>{
    const {data} = await findSports()
    sporList.value = data
}

//清除
const falseAll = ()=>{
    movement.value = {
        id:'',
        sportsId:'',
        name:'',
        explain:'',
        expend:'',
        minutes:'',
        whether:''
    }
    dialogVisible.value = false
}


const deleteById = async (row)=>{
    ElMessageBox.confirm('此操作将永久删除该记录, 是否继续?', 'Warning', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(async () => {
       const {code } = await DeleteMovement(row.id)
       if(code === 200) {
            ElMessage.success('删除成功')
            findMovements()
       }
    })
}
//根据id查询运动信息
const editShow = async(row)=>{
    const {data} = await findMovementById(row.id)
    movement.value = data
    dialogVisible.value = true
}

//打开添加弹框
const addMovement = ()=>{
    dialogVisible.value = true
}
//提交信息时判断是添加还是修改
const submit = async()=>{
    if(!movement.value.id){
        //如果没有id就是添加
        const {code} = await addmovement(movement.value)
        if(code === 200) {
            dialogVisible.value = false
            ElMessage.success('操作成功')
            findMovements()
        }
    }else{
        //有id就是修改
        const {code} = await updateMovement(movement.value)
        if(code === 200){
            dialogVisible.value = false
            ElMessage.success('操作成功')
            findMovements()
        }
    }
}

</script>

<style lang="scss" scoped>

</style>